/**
 * 
 */
package jgrit.gui;

import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 * Class to handle the selecting of image files.
 * 
 * @author Sam Hartsfield, Derek Olejnik
 */
public class FileSelector {

	/**
	 * Extensions of files that we recognize as images.
	 */
	private static final Collection<String> ACCEPTED_TYPES = 
		Arrays.asList(new String[]{
				"bmp",
				"gif",
				"jpg",
				"jpeg",
				"jif",
				"jpe",
				"png",
				"tga",
				"targa",
				"tif",
				"tiff"
		});
	
	/**
	 * Output message if an invalid file is selected
	 */
	private static final String ERROR_OUTPUT = "JGrit only accepts the following filetypes:\n" +
			ACCEPTED_TYPES;
	
	
	JFileChooser chooser = new JFileChooser();
	private FileDialog selector;
	
	private FileDialog getFileDialog(final Frame parent) {
		if (selector == null) {
			selector = new FileDialog(parent, 
					"Please choose a file for conversion", FileDialog.LOAD);
		}
		return selector;
	}
	
	/**
	 * @return The absolute path of the file that was selected, or null
	 */
	public String selectImageFile(final Frame parent) {		
		String selectedFile = ""; //Will hold absolute path to file
		//Get the selected file to import
		if (System.getProperty("os.name").contains("Linux")) {
			int returnVal = chooser.showOpenDialog(parent);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				selectedFile = chooser.getSelectedFile().getAbsolutePath();
			}
		} else {
			FileDialog selector = getFileDialog(parent);
			
			//Filename filters don't work with Sun's implementation w/ Windows,
			//will verify type after it's selected
			selector.setVisible(true);
			if (selector.getFile() != null) {
				selectedFile = selector.getDirectory() + File.separator + 
					selector.getFile();
			}
		}		
		
		if (!verifyFileType(selectedFile)) {
			return null;
		}
		
		String fileType = selectedFile;
		fileType = fileType.substring(fileType.lastIndexOf('.') + 1, 
				fileType.length());
		if (!ACCEPTED_TYPES.contains(fileType)) {
			//They selected an invalid type
			JOptionPane.showMessageDialog(null, ERROR_OUTPUT, "Invalid Filetype", 
					JOptionPane.INFORMATION_MESSAGE);
			return null;
		}
		return selectedFile;
	}

	/**
	 * Verifies that the file is of a valid type, outputs an error otherwise
	 * @param fileName - string filename (include filetype as well)
	 * @return boolean - true if the file type is valid for our software
	 */
	public static boolean verifyFileType(String fileName) {
		if (fileName.lastIndexOf('.') == -1)
			return false;
		
		String fileType = fileName.substring(fileName.lastIndexOf('.') + 1, 
				fileName.length());
		if (!ACCEPTED_TYPES.contains(fileType)) {
			//They selected an invalid type
			JOptionPane.showMessageDialog(null, ERROR_OUTPUT, "Invalid Filetype", 
					JOptionPane.INFORMATION_MESSAGE);
			return false;
		}
		return true;
	}
}
